package cn.acewill.yxd.yunxundian.ui.sign.Presenter;

import android.view.View;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.adapter.WriteEvaluationAdapter;
import cn.acewill.yxd.yunxundian.model.Employee;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.BackUpLoadView;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.ImgPresenter;

/**
 * Created by xxx on 2018/1/21.
 */

public class ImgPresenterIml implements ImgPresenter {

    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private BackUpLoadView loadView;
    public ImgPresenterIml(BackUpLoadView loadView){
        this.loadView = loadView;
    }

    @Override
    public void loadJpgList(int schId, int rowId, BackUpLoadView loadView)  {
        if (loadView!=null){
            tempLostRR rr = null;
            try {
                rr = db.selector(tempLostRR.class).where("schId","=",schId)
                        .and("rRowId","=",rowId).findFirst();
                if (rr.getJpgStr()!=null&&rr.getJpgStr().length()>0){
                    List<String> pic=new ArrayList<>();
                    String[] jpg=rr.getJpgStr().split(",");
                    //由于是本地加载，判断用户是否删除文件，如果删除这移出此项
                    for (String aJpg : jpg) {
                        // TODO: 2017/7/6  1.上传时处理没有图片的问题，2.此处处理,没有图片的删除
                        File file = new File(aJpg);
                        if (file.exists()) {
                            pic.add(aJpg);
                        }
                    }
                    loadView.updateGridList(pic);
                }
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void loadVideoList(int schId, int rowId, BackUpLoadView loadView) {

        if (loadView!=null){
            try {
                tempLostRR rr = db.selector(tempLostRR.class).where("schId","=",schId)
                        .and("rRowId","=",rowId).findFirst();
                if (rr.getVideoStr()!=null&&rr.getVideoStr().length()>0){
                    String[] video=rr.getVideoStr().split(",");
                    List<String> list=new ArrayList<>();
                    for (String aVideo : video) {
                        File file = new File(aVideo);
                        if (file.exists()) {
                            list.add(aVideo);
                        }
                    }
                    loadView.updateGridList(list);
                }
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
    }
}
